package cn.itcast.web.cargo;

import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
@Log4j
public class ExtCproductController extends BaseController {
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private ContractService contractService;
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private FactoryService factoryService;

    /**
     * 1、附件列表
     * 功能入口：购销合同列表点击货物后，进入货物列表再点击附件
     * 请求参数：
     *      contractId 表示购销合同id  （页面存储，添加附件时候使用到）
     *      contractProductId  货物id （页面存储，添加附件时候使用到）
     *                         还需要根据货物查询附件
     */

    @RequestMapping("/list")
    public String list(String contractId, String contractProductId,
                       @RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        log.info("执行附加查询开启");
        //通过货物id查询附件 创建查询对象
        ExtCproductExample example = new ExtCproductExample();

        //创建查询条件
        ExtCproductExample.Criteria criteria = example.createCriteria();

        //根据货物id查询附件
        criteria.andContractProductIdEqualTo(contractProductId);

        //分页
        PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(example, pageNum, pageSize);

        //查询附件货物的厂家  创建查询对象
        FactoryExample factoryExample = new FactoryExample();

        //查询条件
        factoryExample.createCriteria().andCtypeEqualTo("附件");

        //查询所有厂家
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        //保存数据
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);

        return "cargo/extc/extc-list";
    }

    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct){
        //设置企业信息
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());

        //判断是添加还是更新
        if(StringUtils.isEmpty(extCproduct.getId())){
            //保存
            extCproductService.save(extCproduct);
        }else {
            //更新
            extCproductService.update(extCproduct);
        }
        //重定向到list页面
        return "redirect:/cargo/extCproduct/list?contractId="+extCproduct.getContractId()
                +"&contractProductId="+extCproduct.getContractProductId();
    }

    //修改
    @RequestMapping("/toUpdate")
    public String update(String exctId, String contractId,String contractProductId){
        //根据附件id查询附件
        ExtCproduct extCproduct = extCproductService.findById(exctId);

        //查询附件厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        //保存
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("extCproduct",extCproduct);
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-update";
    }

}
